package com.swpu.service_user.controller;


import com.swpu.commonutils.JwtUtils;
import com.swpu.commonutils.R;
import com.swpu.commonutils.ResultEncryption;
import com.swpu.service_user.aop.Decrypt;
import com.swpu.service_user.aop.Encrypt;
import com.swpu.service_user.entity.Test;
import com.swpu.service_user.entity.User;
import com.swpu.service_user.entity.vo.UserAuthor;
import com.swpu.service_user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author caicai
 * @since 2024-01-24
 */
@RestController
@RequestMapping("/userservice/user")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/wxlogin")
    public R login(@RequestBody UserAuthor userAuthor) throws Exception {
        R loginRes = userService.login(userAuthor);
        System.out.println(userAuthor);
        return loginRes;
    }

    @PostMapping("/updateUserStepTarget")
    @Decrypt
    @Encrypt
    public R  updateUserInfoStep(@RequestBody ResultEncryption resultEncryption, @RequestBody User user,HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        User userInfo = userService.updateUserStepCount(user, openid);
        return R.ok().data("userInfo",userInfo);
    }
    @GetMapping("/getUserInfo")
    @Encrypt
    public R getUserInfo(HttpServletRequest request){
        System.out.println(request);
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        User userInfo = userService.getUserInfo(openid);
        return R.ok().data("userinfo",userInfo);
    }
    @PostMapping("updateUserInfo")
    @Decrypt
    @Encrypt
    public R updateUserInfo(@RequestBody ResultEncryption resultEncryption, User user,HttpServletRequest request ){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
         userService.updateUserInfo(openid,user);
        return R.ok();
    }

    @GetMapping("getCaloriesRecommend")
        public R getCaloriesRecommend(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        Map map = userService.getCaloriesRecommend(openid);
        return R.ok().data("recommend",map);
    }
    @GetMapping("/getUserRemindInfo")
    public R getUserRemindInfo(HttpServletRequest request){
        String openid = JwtUtils.getOpenidByJwtToken(request);
        if (openid.isEmpty()){
            return R.error().message("当前用户无访问权限");
        }
        Map map = userService.getUserRemindInfo(openid);
        return R.ok().data("remindInfoMap",map);
    }
    @PostMapping("testjiami")
    @Encrypt
    @Decrypt
    public R testjiami(@RequestBody ResultEncryption resultEncryption, Test test, HttpServletRequest request){

        return R.ok().data("shuju",123);
    }
}

